package com.itboy.mall.wms.waresku.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import com.itboy.mall.wms.waresku.vo.SkuHasStockVo;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.wms.waresku.bo.MallWareSkuAddBo;
import com.itboy.mall.wms.waresku.bo.MallWareSkuQueryBo;
import com.itboy.mall.wms.waresku.bo.MallWareSkuEditBo;
import com.itboy.mall.wms.waresku.domain.MallWareSku;
import com.itboy.mall.wms.waresku.mapper.MallWareSkuMapper;
import com.itboy.mall.wms.waresku.vo.MallWareSkuVo;
import com.itboy.mall.wms.waresku.service.IMallWareSkuService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 商品库存Service业务层处理
 *
 * @author itboy
 * @date 2021-08-10
 */
@Service
public class MallWareSkuServiceImpl extends ServicePlusImpl<MallWareSkuMapper, MallWareSku> implements IMallWareSkuService {

    @Override
    public MallWareSkuVo queryById(Long id){
        return getVoById(id, MallWareSkuVo.class);
    }

    @Override
    public TableDataInfo<MallWareSkuVo> queryPageList(MallWareSkuQueryBo bo) {
        PagePlus<MallWareSku, MallWareSkuVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallWareSkuVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallWareSkuVo> queryList(MallWareSkuQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallWareSkuVo.class);
    }

    private LambdaQueryWrapper<MallWareSku> buildQueryWrapper(MallWareSkuQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallWareSku> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSkuId() != null, MallWareSku::getSkuId, bo.getSkuId());
        lqw.eq(bo.getWareId() != null, MallWareSku::getWareId, bo.getWareId());
        lqw.eq(bo.getStock() != null, MallWareSku::getStock, bo.getStock());
        lqw.like(StrUtil.isNotBlank(bo.getSkuName()), MallWareSku::getSkuName, bo.getSkuName());
        lqw.eq(bo.getStockLocked() != null, MallWareSku::getStockLocked, bo.getStockLocked());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallWareSkuAddBo bo) {
        MallWareSku add = BeanUtil.toBean(bo, MallWareSku.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallWareSkuEditBo bo) {
        MallWareSku update = BeanUtil.toBean(bo, MallWareSku.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallWareSku entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {
		List<SkuHasStockVo> skuHasStockVos = skuIds.stream().map(item -> {
			Long count = this.baseMapper.getSkuStock(item);
			SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
			skuHasStockVo.setSkuId(item);
			skuHasStockVo.setHasStock(count == null?false:count > 0);
			return skuHasStockVo;
		}).collect(Collectors.toList());
		return skuHasStockVos;
	}
}
